using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIGmOutside : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIGmOutside
        {
            #region ObjectBinding Generate
            public UnityEngine.UI.ExLoopVerticalScrollRect scroll { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("scroll", out var __tbv0);
                this.scroll = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv1);
                this.btnClose = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIGmOutside),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields

        private LoopScrollAdapter<UIN_GmOutsideCell> cells;
        #endregion fields

        #region properties

        protected UIB_UIGmOutside ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIGmOutside();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.cells = this.InitLoopScroll<UIN_GmOutsideCell>(this.ui.scroll, this.OnCellChanged);
        }

        protected override void OnShow(UIBaseData data)
        {
            ShowView();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.btnClose.onClick.AddListener(this.Close);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds

        private void ShowView()
        {
            this.ShowList();
        }

        private void ShowList()
        {
            int cnt = TableCenter.gmOutside.DataList.Count;
            this.cells.RefillCells(cnt);
        }

        private void OnCellChanged(UIN_GmOutsideCell item, int index)
        {
            CfgGmPanelList cfg = TableCenter.gmOutside.DataList[index];
            item.Show(cfg, OnOpenCloseCfg);
        }


        private void OnOpenCloseCfg(bool isOn, CfgGmPanelList cfg)
        {
            DevelopSettingModule.Instance.Save(cfg.Type, isOn);
        }
        #endregion methonds
    }
}
